<?php
/*
Plugin Name: YuPooAlbum
Version: 1.0.0
Plugin URI: http://zhangsf.yupoo.com
Description: A plugin for displaying your <a href="http://www.yupoo.com/">yupoo</a> photosets and photos in a gallery format on your Wordpress site.
Author: OSCAR Zhangsf
Author URI: http://zhangsf.yupoo.com

Change log:
1.0 - 	Init Release

Copyright (c) 2007
This file is part of WordPress.
WordPress is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

include_once(ABSPATH . 'wp-includes/streams.php');
include_once(ABSPATH . 'wp-includes/gettext.php');

require_once('ypalbum.php');

define ('YPALBUM_DOMAIN', '/ypalbum/lang/falbum');

load_plugin_textdomain(YPALBUM_DOMAIN);

// plugin menu
function ypalbum_add_pages () {
	if (function_exists('add_options_page')) {
		add_submenu_page('plugins.php', 'YupooAlbum', 'YupooAlbum', 10, basename(__FILE__), 'ypalbum_options_page');
	}
}

function ypalbum_options_page() {

	global $is_apache;
	global $wpdb;

	//

	$wpdb->query("CREATE TABLE IF NOT EXISTS ypalbum_cache (
			ID varchar(40) PRIMARY KEY,
			data text,
			expires datetime
			)");

	//

	$ypalbum_options = get_option('ypalbum_options');

	//

	//$home_path = parse_url("/");
	//$home_path = $home_path['path'];
	//$root2 = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]);
	//$home_path = trailingslashit($root2 . $home_path);

	//

	$urlinfo = parse_url(get_settings('siteurl'));
	$path = $urlinfo['path'];
	$domain = $urlinfo['host'];

	$furl = trailingslashit($ypalbum_options['ypalbum_url_root']);
	if (strpos($furl, "/") == 0) {
		$furl =  substr($furl, 1);
	}

	$pos = strpos('/'.$furl, $path.'/');
	if (strpos('/'.$furl, $path.'/') === false) {
		$home_path = parse_url("/");
		$home_path = $home_path['path'];
		$root2 = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]);
		$home_path = trailingslashit($root2 . $home_path);
	} else {
		$furl = str_replace($path.'/', '', '/'.$furl);
		$home_path = get_home_path();
	}
	if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') ) {
		$writable = true;
	} else {
		$writable = false;
	}
	if (strpos($furl, "/") == 0) {
		$furl =  substr($furl, 1);
	}

	$rewriteRule =
	"AddType text/x-component .htc\n".
	"<IfModule mod_rewrite.c>\n".
	"RewriteEngine On\n".
	"RewriteRule ^".$furl."?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?$ ".$path."/wp-content/plugins/ypalbum/ypalbum-wp.php?$1=$2&$3=$4&$5=$6&$7=$8 [QSA,L]\n".
	"</IfModule>";

	//echo '<pre>$path-'.$path.'/'.'</pre>';
	//echo '<pre>$furl-'.'/'.$furl.'</pre>';
	//echo '<pre>1-'.strpos('/'.$furl, $path.'/').'</pre>';
	//echo '<pre>$furl-'.$furl.'</pre>';
	//echo '<pre>'.$rewriteRule.'</pre>';


	// posting logic
	if (isset($_POST['Submit'])) {

		$ypalbum_options['ypalbum_tsize'] = $_POST['ypalbum_tsize'];
		$ypalbum_options['ypalbum_show_private'] = $_POST['ypalbum_show_private'];
		$ypalbum_options['ypalbum_friendly_urls'] = 'false';
		$ypalbum_options['ypalbum_url_root'] = $_POST['ypalbum_url_root'];
		$ypalbum_options['ypalbum_albums_per_page'] = $_POST['ypalbum_albums_per_page'];
		$ypalbum_options['ypalbum_photos_per_page'] = $_POST['ypalbum_photos_per_page'];
		$ypalbum_options['ypalbum_max_photo_width'] = $_POST['ypalbum_max_photo_width'];
		$ypalbum_options['ypalbum_display_dropshadows'] = $_POST['ypalbum_display_dropshadows'];
		$ypalbum_options['ypalbum_display_sizes'] = $_POST['ypalbum_display_sizes'];
		$ypalbum_options['ypalbum_display_exif'] = $_POST['ypalbum_display_exif'];
		$ypalbum_options['ypalbum_wp_user_level'] = $_POST['ypalbum_wp_user_level'];
		$ypalbum_options['ypalbum_number_recent'] = $_POST['ypalbum_number_recent'];

		$furl = $ypalbum_options['ypalbum_url_root'];
		$pos = strpos($furl, '/');
		if ($pos === false || $pos != 0) {
			$furl =  '/'.$furl;
		}
		$pos = strpos($furl, '.php');
		if ($pos === false) {
			$furl =  trailingslashit($furl);
		}

		$ypalbum_options['ypalbum_url_root'] = $furl;

		update_option('ypalbum_options', $ypalbum_options);

		$updateMessage .= __('Options saved', YPALBUM_DOMAIN)."<br /><br />";

		if ($ypalbum_options['ypalbum_friendly_urls'] == 'true') {

			if ( $is_apache ) {

				if ($writable) {
					ypalbum_insert_with_markers($home_path.'.htaccess', 'YupooAlbum', explode("\n", ""));
				}

				$urlinfo = parse_url(get_settings('siteurl'));
				$path = $urlinfo['path'];
				$domain = $urlinfo['host'];

				$furl = trailingslashit($ypalbum_options['ypalbum_url_root']);
				if (strpos($furl, "/") == 0) {
					$furl =  substr($furl, 1);
				}

				//echo '<pre>$path-'.$path.'/'.'</pre>';
				//echo '<pre>$furl-'.'/'.$furl.'</pre>';
				//echo '<pre>1-'.strpos('/'.$furl, $path.'/').'</pre>';

				$pos = strpos('/'.$furl, $path.'/');

				if ($path != '/' && strpos('/'.$furl, $path.'/') === false) {
					//use root .htaccess file
					//echo '<pre>root</pre>';
					$home_path = parse_url("/");
					$home_path = $home_path['path'];
					$root2 = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]);
					$home_path = trailingslashit($root2 . $home_path);
				} else {
					//use wp .htaccess file
					//echo '<pre>wp</pre>';
					if (strlen($path) > 1) {
						$furl = str_replace($path.'/', '', '/'.$furl);
					}
					$home_path = get_home_path();
				}
				if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') ) {
					$writable = true;
				} else {
					$writable = false;
				}
				if (strpos($furl, "/") == 0) {
					$furl =  substr($furl, 1);
				}

				$rewriteRule =
				"AddType text/x-component .htc\n".
				"<IfModule mod_rewrite.c>\n".
				"RewriteEngine On\n".
				"RewriteRule ^".$furl."?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?$ ".$path."/wp-content/plugins/ypalbum/ypalbum-wp.php?$1=$2&$3=$4&$5=$6&$7=$8 [QSA,L]\n".
				"</IfModule>";

				//echo '<pre>$home_path-'.$home_path.'</pre>';
				//echo '<pre>'.$rewriteRule.'</pre>';

				if ($writable) {
					$rules = explode("\n", $rewriteRule);

					ypalbum_insert_with_markers($home_path.'.htaccess', 'YupooAlbum', $rules);

					$updateMessage .= __('Mod rewrite rules updated', YPALBUM_DOMAIN)."<br /><br />";

					//

					$wpdb->query("DELETE from ypalbum_cache");

					$updateMessage .= __('Cache cleared', YPALBUM_DOMAIN)."<br />";

				}
			}
		} else {
			if ($writable) {
				ypalbum_insert_with_markers($home_path.'.htaccess', 'YupooAlbum', explode("\n", ""));
			}
		}
	}


	if (isset($_POST['ClearToken'])) {

		$ypalbum_options['ypalbum_token'] = null;
		update_option('ypalbum_options', $ypalbum_options);

		$updateMessage .= __('yupoo authorization reset', YPALBUM_DOMAIN)."<br />";

	}



	if (isset($_POST['ClearCache'])) {

		$wpdb->query("DELETE from ypalbum_cache");

		$updateMessage .= __('Cache cleared', YPALBUM_DOMAIN)."<br />";

	}


	if (isset($_POST['GetToken'])) {

		$frob2 = $_GET['frob'];
    //echo $frob2;
    //exit;
		$url = 'http://www.yupoo.com/api/rest/?method=yupoo.auth.getToken&api_key='.YPALBUM_API_KEY.'&frob='.$frob2;
		$parms = 'api_key'.YPALBUM_API_KEY.'frob'.$frob2.'methodyupoo.auth.getToken';
		$url = $url.'&api_sig='.md5(YPALBUM_SECRET.$parms);

		//echo '<pre>'.htmlentities($url).'</pre>';

		$resp = fa_fopen_url($url, 0);

		//echo '<pre>'.htmlentities($resp).'</pre>';

		$xpath = fa_parseXPath($resp);
		$token = $xpath->getData("/rsp/auth/token");
		$nsid = $xpath->getData("/rsp/auth/user/@id");

		$ypalbum_options['ypalbum_token'] = $token;
		$ypalbum_options['ypalbum_nsid'] = $nsid;

		update_option('ypalbum_options', $ypalbum_options);

		$updateMessage .= __('Successfully set token', YPALBUM_DOMAIN)."<br />";

	}

	if (isset($updateMessage)) {

		?> <div class="updated"><p><strong><?php echo $updateMessage?></strong></p></div> <?php

	}


	//Init Settings
	if (!isset($ypalbum_options['ypalbum_tsize']) || $ypalbum_options['ypalbum_tsize'] == "") {
		$ypalbum_options['ypalbum_tsize'] = "s";
	}
	if (!isset($ypalbum_options['ypalbum_show_private']) || $ypalbum_options['ypalbum_show_private'] == "") {
		$ypalbum_options['ypalbum_show_private'] = "false";
	}
	if (!isset($ypalbum_options['ypalbum_friendly_urls']) || $ypalbum_options['ypalbum_friendly_urls'] == "") {
		$ypalbum_options['ypalbum_friendly_urls'] = "false";
	}
	if (!isset($ypalbum_options['ypalbum_url_root']) || $ypalbum_options['ypalbum_url_root'] == "") {
		$ypalbum_options['ypalbum_url_root'] = $path."/wp-content/plugins/ypalbum/photos.php";
	}
	if (!isset($ypalbum_options['ypalbum_albums_per_page']) || $ypalbum_options['ypalbum_albums_per_page'] == "") {
		$ypalbum_options['ypalbum_albums_per_page'] = "10";
	}
	if (!isset($ypalbum_options['ypalbum_photos_per_page']) || $ypalbum_options['ypalbum_photos_per_page'] == "") {
		$ypalbum_options['ypalbum_photos_per_page'] = "45";
	}
	if (!isset($ypalbum_options['ypalbum_max_photo_width']) || $ypalbum_options['ypalbum_max_photo_width'] == "") {
		$ypalbum_options['ypalbum_max_photo_width'] = "450";
	}
	if (!isset($ypalbum_options['ypalbum_display_dropshadows']) || $ypalbum_options['ypalbum_display_dropshadows'] == "") {
		$ypalbum_options['ypalbum_display_dropshadows'] = "-nods";
	}
	if (!isset($ypalbum_options['ypalbum_display_sizes']) || $ypalbum_options['ypalbum_display_sizes'] == "") {
		$ypalbum_options['ypalbum_display_sizes'] = "false";
	}
	if (!isset($ypalbum_options['ypalbum_display_exif']) || $ypalbum_options['ypalbum_display_exif'] == "") {
		$ypalbum_options['ypalbum_display_exif'] = "false";
	}
	if (!isset($ypalbum_options['ypalbum_wp_user_level']) || $ypalbum_options['ypalbum_wp_user_level'] == "") {
		$ypalbum_options['ypalbum_wp_user_level'] = "10";
	}
	if (!isset($ypalbum_options['ypalbum_number_recent']) || $ypalbum_options['ypalbum_number_recent'] == "") {
		$ypalbum_options['ypalbum_number_recent'] = "-1";
	}

?>


<div class="wrap">

<script language='javascript'>
function check(){
	if (bind.apikey.value=="" || bind.sharedkey.value==""){
		alert('请输入完整信息');
		return false;
	}
}
</script>
  <h2>Yupoo 设置</h2>
  <?php if (!isset($ypalbum_options['ypalbum_token']) || $ypalbum_options['ypalbum_token'] == '' ) { ?>

  <form method=post action="../ypbind.php" name="bind" onsubmit="return check();">
   <fieldset class="options">
       <legend><?php _e('Initial Setup', YPALBUM_DOMAIN);?></legend>
        
               <?php

               /*$url = 'http://www.yupoo.com/api/rest/?method=yupoo.auth.getFrob&api_key='.YPALBUM_API_KEY;
               $parms = 'api_key'.YPALBUM_API_KEY.'methodyupoo.auth.getFrob';
               $url = $url.'&api_sig='.md5(YPALBUM_SECRET.$parms);

               //echo '<pre>$url-'.htmlentities($url).'</pre>';

               $resp = fa_fopen_url($url, 0);

               //echo '<pre>$resp-'.htmlentities($resp).'</pre>';

               $xpath = fa_parseXPath($resp);
               $frob = $xpath->getData("/rsp/frob");

               //echo '<pre>$frob-'.htmlentities($frob).'</pre>';
               */
               $link = 'http://www.yupoo.com/services/auth/?api_key='.YPALBUM_API_KEY.'&perms=write';
               
               $parms = 'api_key'.YPALBUM_API_KEY.'permswrite';
               $link .= '&api_sig='.md5(YPALBUM_SECRET.$parms);

       ?>
       
	      <input type="hidden" name="frob" value="<?php echo $_GET["frob"]?>" >
	      
	      <p>	      
	      <?php _e('请完成以下步骤以使YupooFalbum能调用您的Yupoo照片。', YPALBUM_DOMAIN);?>
	      </p>
       <p>
	   <?php _e('Step 1:', YPALBUM_DOMAIN);?><?php _e('批准YupooFalbum使用你的Yupoo账号 ', YPALBUM_DOMAIN);?>
	   </p>
        <p>
        API KEY:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="apikey" type="text" size="30" value="<?=YPALBUM_API_KEY?>" />
       	</p>
		<p>
        SHARED KEY:&nbsp;<input name="sharedkey" type="text" size="30" value="<?=YPALBUM_SECRET?>" />
       	</p>
       <input name="rebind" type="submit" value="绑定API KEY" />
       <input name="rebind" type="button" value="申请API KEY" onclick="javascript:self.open('http://www.yupoo.com/services/api/apply','_blank')"/>
                       
      </fieldset>
  </form>
  <?}?>
  
    <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>?page=ypalbum-plugin.php&frob=<?=$_GET["frob"]?>">
        <input type="hidden" name="update" value="true">
                       
        <?php if (!isset($ypalbum_options['ypalbum_token']) || $ypalbum_options['ypalbum_token'] == '' ) { ?>

       <fieldset class="options">
       <legend><?//php _e('Initial Setup', YPALBUM_DOMAIN);?></legend>
        
               <?php

               /*$url = 'http://www.yupoo.com/api/rest/?method=yupoo.auth.getFrob&api_key='.YPALBUM_API_KEY;
               $parms = 'api_key'.YPALBUM_API_KEY.'methodyupoo.auth.getFrob';
               $url = $url.'&api_sig='.md5(YPALBUM_SECRET.$parms);

               //echo '<pre>$url-'.htmlentities($url).'</pre>';

               $resp = fa_fopen_url($url, 0);

               //echo '<pre>$resp-'.htmlentities($resp).'</pre>';

               $xpath = fa_parseXPath($resp);
               $frob = $xpath->getData("/rsp/frob");

               //echo '<pre>$frob-'.htmlentities($frob).'</pre>';
               */
               $link = 'http://www.yupoo.com/services/auth/?api_key='.YPALBUM_API_KEY.'&perms=write';
               
               $parms = 'api_key'.YPALBUM_API_KEY.'permswrite';
               $link .= '&api_sig='.md5(YPALBUM_SECRET.$parms);

       ?>
       
	      <input type="hidden" name="frob" value="<?php echo $_GET["frob"]?>">
	      
	      <!--<p>	      
	      <?//php _e('请完成以下步骤以使YupooFalbum能调用您的Yupoo照片。', YPALBUM_DOMAIN);?>
	      </p>
       
        <p>
         <?//php _e('Step 1:', YPALBUM_DOMAIN);?> <a href="<?php echo $link?>" target="_blank"><?php _e('批准YupooFalbum使用你的Yupoo账号 ', YPALBUM_DOMAIN);?></a>
       	</p>
		-->
       	       	
       	 
       	 <p>
         <?php _e('Step 2:', YPALBUM_DOMAIN);?> <input type="submit" name="GetToken" value="<?php _e('Get Authentication Token', YPALBUM_DOMAIN);?>" />
       	</p>
       	
                       
      </fieldset>
      
      	<?php } ?>
      
      
 <fieldset class="options">
       <legend><?php _e('YupooAlbum Admin', YPALBUM_DOMAIN);?></legend>
         
        <p>
         <input type="submit" name="ClearCache" value="<?php _e('Clear Cache', YPALBUM_DOMAIN);?>" />
         &nbsp;&nbsp;&nbsp;
         
         <?php if (isset($ypalbum_options['ypalbum_token'])) { ?>
         <input type="submit" name="ClearToken" value="<?php _e('重新设置 YuPoo 认证', YPALBUM_DOMAIN);?>" />
         <?php } ?>
         
       </p>
	   
	   
      </fieldset>
       <p align="right">相册地址:<a herf="<?='http://'.$_SERVER["HTTP_HOST"].$ypalbum_options['ypalbum_url_root'] ?>" class="current"><?='http://'.$_SERVER["HTTP_HOST"].$ypalbum_options['ypalbum_url_root'] ?></a><input type="button" name="Submit" value="显示相册" onclick="javascript:self.open('<?='http://'.$_SERVER["HTTP_HOST"].$ypalbum_options['ypalbum_url_root'] ?>','_blank')"/></p>
       <hr />
       
        <fieldset class="options">
            <legend><?php _e('Set Up', YPALBUM_DOMAIN);?></legend>
            <table width="100%" cellspacing="2" cellpadding="5" class="editform">

            	<tr valign="top">
            	<th width="33%" scope="row"><?php _e('Thumbnail Size', YPALBUM_DOMAIN);?>:</th>
                    <td>
                    <select name="ypalbum_tsize">
                    <option value="s"<?php if ($ypalbum_options['ypalbum_tsize'] == 's') { ?> selected="selected"<?php } ?> ><?php _e('Square', YPALBUM_DOMAIN);?> (75px x 75px)</option>
                    <option value="t"<?php if ($ypalbum_options['ypalbum_tsize'] == 't') { ?> selected="selected"<?php } ?> ><?php _e('Thumbnail', YPALBUM_DOMAIN);?> (100px x 75px)</option>
                    <option value="m"<?php if ($ypalbum_options['ypalbum_tsize'] == 'm') { ?> selected="selected"<?php } ?> ><?php _e('Small', YPALBUM_DOMAIN);?> (240px x 180px)</option>
                    </select><br />
                    <?php _e('Size of the thumbnail you want to appear in the album thumbnail page', YPALBUM_DOMAIN);?><br /></td>
                </tr>
                
                <tr valign="top">
                    <th width="33%" scope="row"><?php _e('Albums Per Page', YPALBUM_DOMAIN);?>:</th>
                    <td><input type="text" name="ypalbum_albums_per_page" size="3" value="<?php echo $ypalbum_options['ypalbum_albums_per_page'] ?>"/><br />
                   <?php _e('How many albums to show on a page (0 for no paging)', YPALBUM_DOMAIN);?></td>
                </tr>
                <tr valign="top">
                    <th width="33%" scope="row"><?php _e('Photos Per Page', YPALBUM_DOMAIN);?>:</th>
                    <td><input type="text" name="ypalbum_photos_per_page" size="3" value="<?php echo $ypalbum_options['ypalbum_photos_per_page'] ?>"/><br />
                   <?php _e('How many photos to show on a page (0 for no paging)', YPALBUM_DOMAIN);?></td>
                </tr>
                <tr valign="top">
                    <th width="33%" scope="row"><?php _e('Recent Images', YPALBUM_DOMAIN);?>:</th>
                    <td><input type="text" name="ypalbum_number_recent" size="3" value="<?php echo $ypalbum_options['ypalbum_number_recent'] ?>"/><br />
                   <?php _e('How many of the most recent photos to show (0 for no recent images / -1 to show all available images)', YPALBUM_DOMAIN);?></td>
                </tr>
				
		<tr valign="top">
                    <th width="33%" scope="row"><?php _e('Max Photo Width', YPALBUM_DOMAIN);?>:</th>
                    <td><input type="text" name="ypalbum_max_photo_width" size="3" value="<?php echo $ypalbum_options['ypalbum_max_photo_width'] ?>"/><br />
                   <?php echo "照片最长边宽度(单位：像素)。 (0 表示不需要设定)   默认尺寸一般设置为500像素，从YuPoo调用过来的照片尺寸就为500像素。"?></td>
              </tr>  
                
                 <tr valign="top">
                    <th width="33%" scope="row"><?php _e('Display Drop Shadows', YPALBUM_DOMAIN);?>:</th>
                    <td>
                    <select name="ypalbum_display_dropshadows">
                    <option value="-ds"<?php if ($ypalbum_options['ypalbum_display_dropshadows'] == '-ds') { ?> selected="selected"<?php } ?> ><?php _e('true', YPALBUM_DOMAIN);?></option>
                    <option value="-nods"<?php if ($ypalbum_options['ypalbum_display_dropshadows'] == '-nods') { ?> selected="selected"<?php } ?> ><?php _e('false', YPALBUM_DOMAIN);?></option>
                    </select>
                    <br />
                    <?php _e('Whether or not to show drop shadows under photos', YPALBUM_DOMAIN);?></td>
                </tr>
                
               <tr valign="top">	
                    <th width="33%" scope="row"><?php _e('URL Root', YPALBUM_DOMAIN);?>:</th>
                    <td><input type="text" name="ypalbum_url_root" size="60" value="<?php echo $ypalbum_options['ypalbum_url_root'] ?>"/><br />
                   <?php 
//_e('URL to use as the root for all navigational links.<br /><strong>NOTE:</strong>It is important that you specify something here, for example:<br />If friendly URLs is <strong>enabled</strong> use - /photos/<br /> If friendly URLs is <strong>disabled</strong> use - ', YPALBUM_DOMAIN); 
//echo $path."/wp-content/plugins/ypalbum/ypalbum-wp.php" 
?>
				   </td>
              </tr>
                 <!--<tr valign="top">
                    <th width="33%" scope="row"><?php// _e('Rewrite Rules', YPALBUM_DOMAIN);?></th>
                    <td>
                    <?php if ( !$writable && $is_apache) : ?>
  <p><?php _e('If your', YPALBUM_DOMAIN);?><code><?php echo $home_path?>.htaccess</code><?php _e('file was <a href="http://codex.wordpress.org/Make_a_Directory_Writable">writable</a> we could do this automatically, but it isn\'t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
  <p><textarea rows="5" style="width: 98%;" name="rules"><?php echo $rewriteRule; ?>
  </textarea></p><?php endif; ?>    
					</td>
                </tr>-->        
				                                 
            </table>
     
       <p class="submit">
         <input type="submit" name="Submit" value="<?php _e('Update Options', YPALBUM_DOMAIN);?> &raquo;" />
		 <input type="button" name="Submit" value="显示相册" onclick="javascript:self.open('<?='http://'.$_SERVER["HTTP_HOST"].$ypalbum_options['ypalbum_url_root'] ?>','_blank')"/>
       
       </p>
       
	</fieldset>
   
    </form>
</div>

<?php

}

// function for outputting header information
//
function ypalbum_header() {
	$hHead = "<meta name=\"generator\" content=\"YupooAlbum 1.0.0\" />\n";
	$hHead .= "\n<style type=\"text/css\">\n";
	$hHead .= "@import url(".get_settings('siteurl')."/wp-content/plugins/ypalbum/ypalbum.css);\n";
	$hHead .= "</style>\n";
	print($hHead);
}

//Temp Method - needed to fix WordPress issue
function ypalbum_insert_with_markers($filename, $marker, $insertion) {
	if (!file_exists($filename) || is_writeable($filename)) {
		if (!file_exists($filename)) {
			$markerdata = '';
		} else {
			$markerdata = explode("\n", implode('', file($filename)));
		}

		$f = fopen($filename, 'w');
		$foundit = false;
		if ($markerdata) {
			$state = true;
			foreach ($markerdata as $markerline) {
				if (strstr($markerline, "# BEGIN {$marker}"))
				$state = false;
				if ($state)
				fwrite($f, "{$markerline}\n");
				if (strstr($markerline, "# END {$marker}")) {
					fwrite($f, "# BEGIN {$marker}\n");
					if (is_array($insertion))
					foreach ($insertion as $insertline)
					fwrite($f, "{$insertline}\n");
					fwrite($f, "# END {$marker}\n");
					$state = true;
					$foundit = true;
				}
			}
		}
		if (!$foundit) {
			fwrite($f, "\n# BEGIN {$marker}\n");
			foreach ($insertion as $insertline)
			fwrite($f, "{$insertline}\n");
			fwrite($f, "# END {$marker}\n");
		}
		fclose($f);
		return true;
	} else {
		return false;
	}
}
//

// output styles to the <head> section of the page
add_action('wp_head', 'ypalbum_header');
add_action('admin_menu', 'ypalbum_add_pages');

?>
